package com.feature.core.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.feature.core.util.event.AfterTransactionEvent;

public class SpringContextUtil implements ApplicationContextAware {
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public static <T> T getBean(Class<T> cls) {
        return context.getBean(cls);
    }

    public static <T> T getBean(String name) {
        return (T) context.getBean(name);
    }

    public static void publishEvent(Object event) {
        context.publishEvent(event);
    }
    
    public static void publishAfterTransactionEvent(Runnable runnable) {
        context.publishEvent(new AfterTransactionEvent(runnable));
    }
}
